package peace.util
{
import flash.events.EventDispatcher;
import flash.events.StatusEvent;
import flash.net.LocalConnection;
import flash.system.Security;
import flash.text.TextField;
/**
 * 
 * @author peace
 * 
 */
public class Logger extends EventDispatcher
{
	private static const DEBUG:uint = 0;
	private static const ALL:uint = 1;
	private static const WARNINGS:uint = 2;
	private static const ERRORS:uint = 3;
	private static const NOTHING:uint = 4;
	/**
	 * потенциальный клиент 
	 */
	protected static var client:TextField = null;
	/**
	 * 
	 */	
	protected static var level:uint = ALL;
	
	/**
	 * 
	 */
	private static const DELIMITER:String = ", ";
	/**
	 * 
	 */	
	private static var enabled:Boolean = true;
	/**
	 * 
	 */
	private static var lc:LocalConnection = new LocalConnection();
	/**
	* активен ли LOcalConnection
	*/	
	private static var localConnectionEnabled:Boolean = false;

	/**
	 * 
	 * @param args
	 * 
	 */	
	public static function debug (... args):void 
	{
		if (level <= DEBUG) 
		{
			sendToDebugger(args.join(DELIMITER));
		}
	}
	/**
	 * 
	 * @param args
	 * 
	 */	
	public static function _msg (... args):void 
	{
		if (level == ALL) 
		{
			sendToDebugger(args.join(DELIMITER));		
		}
	}
	/**
	 * 
	 * @param args
	 * 
	 */
	public static function msg (... args):void 
	{
		if (level == ALL) 
		{
			sendToDebugger(args.join(DELIMITER));		
		}
	}
	/**
	 * 
	 * @param args
	 * 
	 */
	public static function error (... args):void 
	{
		if (level <= ERRORS) 
		{
			sendToDebugger(args.join(DELIMITER));
		}
	}
	/**
	 * 
	 * @param args
	 * 
	 */
	public static function warning (... args):void 
	{
		if (level <= WARNINGS) 
		{
			sendToDebugger(args.join(DELIMITER));
		}
	}
	/**
	 * 
	 * @param message
	 * 
	 */	
	private static function sendToDebugger (message:String):void 
	{
		if (enabled) 
		{
			trace (message);
			
			if (client)
			{
				client.appendText(message + "\n");
			}
			
			if (localConnectionEnabled)
			{
				sendToLocalConnection(message);
			}
		}
	}
	/**
	 * 
	 * @param message
	 * 
	 */	
	public static function sendToLocalConnection (message:*):void
	{
		if(! lc.hasEventListener(StatusEvent.STATUS)) 
		{
			lc.addEventListener(StatusEvent.STATUS, onLCStatus);
			Security.allowDomain("*");
			lc.allowDomain("*");
		}
		lc.send("LoggerOutput", "log", String(message));
	}
	/**
	 * 
	 * @param event
	 * 
	 */	
	private static function onLCStatus(event:StatusEvent):void
	{
//		msg(event);
	}
	/**
	 * 
	 * @param enabled
	 * @param localConnectionEnabled
	 * 
	 */	
	public static function setEnabled (enabled:Boolean, localConnectionEnabled:Boolean=false):void
	{
		Logger.enabled = enabled;
		Logger.localConnectionEnabled = localConnectionEnabled;
	}
	/**
	 * Устанавливаем поле-клиент для вывода <br>
	 * 
	 * Если null - отвязываем текущий клиент
	 * 
	 * @param t
	 * 
	 */	
	public static function setClient (t:TextField):void
	{
		client = t;
	}
		
	

}

}

